<?php 

namespace Common\Controller;

define("HELLYEMS_VERSION", C("HELLYEMS_VERSION"));

class WeixinBase extends CommonSet {
    
    protected function _initialize() {
        parent::_initialize();
        //$this->getUserInfo();
        $this->assign('ver', '201602181809');
    }
    
    //获取登录用户的信息
    final public function getUserInfo(){
        //service('Passport')->userid = 49;
        if(service('Passport')->userid){
            $this->userid = (int)service("Passport")->userid;
            $this->uname = service("Passport")->uname;
            $this->userinfo = service("Passport")->getInfo();
            $this->assign('userinfo',$this->userinfo);
        }
    }
    
    final public function isLogin($type = 0)
    {
        if(!$this->userid)
        {
            if($type == 1)
            {
                $this->error('请先登录', U('Login/index'));
            }
            else 
            {
                header('Location:'.WEB_URL.U('Login/index'));
                exit;
            }
        }
        else 
        {
            return $this->userid;
        }
    }
    
    public function getsysmsgnum()
    {
        $arr = array();
         
        $message1 = M('UserMessage')->where("ifread=0 and (touserid=".$this->userid." or (type=2 and infoid=".$this->userid."))")->count();
        $message2 = M('UserMessage')->alias('A')->join(C('DB_PREFIX').'works B on A.infoid=B.workid', 'LEFT')->where("A.ifread=0 and A.type=1 and (A.touserid is NULL OR A.touserid<>".$this->userid.") and B.userid=".$this->userid)->count();
        $message3 = M('UserMessage')->alias('A')->join(C('DB_PREFIX').'suipai B on A.infoid=B.id', 'LEFT')->where("A.ifread=0 and A.type=4 and (A.touserid is NULL OR A.touserid<>".$this->userid.") and B.userid=".$this->userid)->count();
        $message4 = M('UserMessage')->alias('A')->join(C('DB_PREFIX').'teachlist B on A.infoid=B.teachid', 'LEFT')->where("A.ifread=0 and A.type=7 and (A.touserid is NULL OR A.touserid<>".$this->userid.") and B.userid=".$this->userid)->count();
        $arr['message'] = $message1 + $message2 + $message3 + $message4;
         
        $dig = M('Dig');
        $fav = M('UserFavorite');
        $dig_count = $dig->alias("A")->join(C('DB_PREFIX').'works B ON A.infoid=B.workid', 'LEFT')->where("B.userid=%d and A.status=1 and A.ifread=0", array($this->userid))->count();
        $fav_count = $fav->alias("A")->join(C('DB_PREFIX').'works B ON A.infoid=B.workid', 'LEFT')->where("B.userid=%d and A.status=1 and A.ifread=0", array($this->userid))->count();
        $arr['digfav'] = $dig_count + $fav_count;
         
        $arr['fans'] = M('UserFans')->where("ifread=0 and userid=%d and status=1", array($this->userid))->count();
         
        $suipai_count = M('Suipai')->where("status!=0 and ifread=0 and userid=%d", array($this->userid))->count();
        $works_count = M('Works')->where("status!=0 and ifread=0 and userid=%d", array($this->userid))->count();
        $arr['pro'] = $suipai_count + $works_count;
         
        $where = "A.ifreadto=0 and B.userid=".$this->userid;
        $arr['copypro'] = M('Works')->alias("A")->join(C('DB_PREFIX')."works B ON A.copyid=B.workid", "LEFT")->where($where)->count();
         
        $arr['score'] = M('UserDynamic')->where("status=1 and ifread=0 and userid=%d", array($this->userid))->count();
         
        $arr['listtry'] = M('TryApply')->where('status <> 0 and userid='.$this->userid." and ifread=0")->count();
         
        $yarr = array();
        $ids = M('Sysmsg')->where('status=1 and sendtype=1 and (userid='.$this->userid." or userid is null) and create_time > '1438358400'")->order('create_time desc')->select();
        if($ids)
        {
            foreach($ids as $k => $v)
            {
                $temp_type = M('SysmsgLog')->where("sysid=".$v['id']." and userid=".$this->userid)->getField('ifread');
                if(!isset($temp_type))
                {
                    $info['sysid'] = $v['id'];
                    $info['userid'] = $this->userid;
                    $info['ifread'] = 0;
                    $info['is_del'] = 0;
                    M('SysmsgLog')->data($info)->add();
                    $yarr[] = $v;
                }
                if($temp_type == 0)
                {
                    $yarr[] = $v;
                }
            }
        }
        $arr['sysmsg'] = count($yarr);
         
        $num = $arr['message'] + $arr['digfav'] + $arr['fans'] + $arr['pro'] + $arr['copypro'] + $arr['score'] + $arr['listtry'] + $arr['sysmsg'];
        return $num;
    }
    
    /**
     * 获取广告
     * @param $menu 所属菜单
     * @param $position 位置
     * @param $from 从第几个开始
     * @param int $num 数量
     * @return mixed 返回数据/数组
     */
    public function getAdvert($menu,$position,$from,$num=1){
        $adModel=M('Advert');
        $where['status']=array('EQ',true);
        $where['menu']=array('EQ',$menu);
        $where['position']=array('EQ',$position);
        $field=array('image,linkurl,title');
        if($num<2){
            $ad=$adModel->where($where)->field($field)->order('sort DESC,id DESC')->limit($from,$num)->find();
        }else{
            $ad=$adModel->where($where)->field($field)->order('sort DESC,id DESC')->limit($from,$num)->select();
        }
        cache('advert',$ad);
        return $ad;
    }
}
